package org.kernely.test.activator;


import org.kernely.core.service.KernelySettingsService;
import org.kernely.persistence.service.SessionService;
import org.kernely.test.testbundle.TestBundle;
import org.kernely.test.todopage.ToDoPresenter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.LogService;

/**
 * Test Bundle Activator
 */
public class Activator implements org.osgi.framework.BundleActivator {

	/**
	 * Called when the bundle start
	 */
	public void start(BundleContext pBundleContext) throws Exception {
		
		// Retrieve Apache Log Service
		ServiceReference ref2 = pBundleContext.getServiceReference(LogService.class.getName());
		if (ref2 != null)
		{
			LogService log = (LogService) pBundleContext.getService(ref2);
			TestBundle.setLog(log);
		}
		// Retrieve the Apache Log Reader Service
		ServiceReference logReader = pBundleContext.getServiceReference(LogReaderService.class.getName());
		if (logReader!= null)
		{
			LogReaderService reader = (LogReaderService) pBundleContext.getService(logReader);
			TestBundle.setLogReader(reader);
		}

		// Retrieve KernelySettingsService service
		ServiceReference ref3 = pBundleContext.getServiceReference(KernelySettingsService.class.getName());
		if (ref3 != null){
			KernelySettingsService service = (KernelySettingsService)pBundleContext.getService(ref3);

			TestBundle.setBundleSettings(service);

		}
		else {
			TestBundle.getLog().log(LogService.LOG_WARNING, "SettingsService reference is not set. May cause troubles.");
		}
		
		// Register icon
		ServiceReference sRef = pBundleContext.getServiceReference(HttpService.class.getName());
	    if (sRef != null)
	    {
	      HttpService service = (HttpService) pBundleContext.getService(sRef);
	      service.registerResources("/testbundle/images", "icon", null);
	    }
	}

	/**
	 * {@inheritDoc}
	 */
	public void stop(BundleContext pBundleContext) throws Exception {

	}

}
